package com.duolebo.uteped_sdk;

import android.util.Log;

import androidx.annotation.NonNull;

import com.duolebo.uteped_sdk.utils.PermissionHelper;
import com.duolebo.uteped_sdk.utils.Tools;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;

/**
 * UTEPedSdkPlugin
 */
public class UTEPedSdkPlugin implements FlutterPlugin, ActivityAware {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private final static String TAG = "UTEPedSdkPlugin";
    private UTEPedMethodCallHandler utePedMethodCallHandler;

    private FlutterPluginBinding flutterPluginBinding;
    private ActivityPluginBinding activityPluginBinding;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        Log.d(TAG,"onAttachedToEngine...");
        this.flutterPluginBinding = flutterPluginBinding;
        Tools.setApplicationContext(flutterPluginBinding.getApplicationContext());
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        this.flutterPluginBinding = null;
    }

    @Override
    public void onAttachedToActivity(ActivityPluginBinding binding) {
        Log.d(TAG,"onAttachedToActivity...");
        this.utePedMethodCallHandler = new UTEPedMethodCallHandler(binding.getActivity(), this.flutterPluginBinding.getBinaryMessenger());
        this.activityPluginBinding = binding;
        Tools.setActivity(binding.getActivity());
//        PermissionHelper.getInstance().checkBluetoothPermissions(binding.getActivity(), aBoolean -> {
//            // callback
//        });
//        Tools.checkNotificationService();
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
        Log.d(TAG,"onDetachedFromActivityForConfigChanges...");
        this.onDetachedFromActivity();
    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
        Log.d(TAG,"onReattachedToActivityForConfigChanges...");
        this.onAttachedToActivity(binding);
    }

    @Override
    public void onDetachedFromActivity() {
        Log.d(TAG,"onDetachedFromActivity...");
        Tools.setActivity(null);
        if (this.utePedMethodCallHandler != null) {
            this.utePedMethodCallHandler.onDetached(this.activityPluginBinding);
            this.utePedMethodCallHandler = null;
            this.activityPluginBinding = null;
        }
    }

}
